package zh.javadesign.工厂方法.impl01;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zh.javadesign.工厂方法.Scene.card.IQiYiCard;
import zh.javadesign.工厂方法.Scene.card.IQiYiCardService;
import zh.javadesign.工厂方法.Scene.coupon.CouponResult;
import zh.javadesign.工厂方法.Scene.coupon.CouponService;
import zh.javadesign.工厂方法.Scene.goods.DeliverReq;
import zh.javadesign.工厂方法.Scene.goods.GoodsService;

/**
 * Copyright (C) 广州中和互联网技术有限公司
 * 模拟发奖服务
 *
 * @author 范家微 fanjiawei@borche.cn
 * @date :2021/1/27 22:35
 */
public class PrizeController {
	private Logger logger = LoggerFactory.getLogger(PrizeController.class);

	public AwardRes awardToUser(AwardReq req) {
		String reqJson = JSON.toJSONString(req);
		AwardRes awardRes = null;
		try {
			logger.info("奖品发放开始{}，req:{}", req.getUId(), reqJson);

			if (req.getAwardType() == 1) {
				CouponService couponService = new CouponService();
				CouponResult couponResult = couponService.sendCoupon(req.getUId(), req.getAwardNumber(), req.getBizId());
				if ("0000".equals(couponResult.getCode())) {
					awardRes = new AwardRes("0000", "发放成功");
				} else {
					awardRes = new AwardRes("0001", couponResult.getInfo());
				}

			} else if(req.getAwardType() == 2) {
				GoodsService goodsService = new GoodsService();
				DeliverReq deliverReq = new DeliverReq();
				deliverReq.setUserName(getUserNameByUid(req.getUId()))
						.setUserPhone(getPhoneByUid(req.getUId()))
						.setSku(req.getAwardNumber())
						.setOrderId(req.getBizId())
						.setConsigneeUserName(req.getExtMap().get("setConsigneeUserName"))
						.setConsifneeUserPhone(req.getExtMap().get("setConsifneeUserPhone"))
						.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
				Boolean isSuccess = goodsService.deliverGoods(deliverReq);
				if (isSuccess) {
					awardRes = new AwardRes("0000", "发放成功");
				}else {
					awardRes = new AwardRes("0001", "发放失败");

				}
			}else if (req.getAwardType()==3){
				String bindMobileNumber = getPhoneByUid(req.getUId());
				IQiYiCardService iQiYiCardService = new IQiYiCardService();
				iQiYiCardService.grantToken(bindMobileNumber,req.getAwardNumber());
				awardRes = new AwardRes("0001", "发放失败");
			}
			logger.info("奖品发放完成{}",req.getUId());
		}catch (Exception e){
			logger.error("奖品发放失败{}。req:{}", req.getUId(), reqJson, e);
			awardRes = new AwardRes("0001", e.getMessage());
		}

		return awardRes;

	}


	private String getUserNameByUid(String uid){
		return "迪丽热巴";
	}
	private String getPhoneByUid(String uid){
		return "123456";
	}
}
